package com.warm.mini.wework.util;

import com.warm.mini.wework.model.JsonConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;

@Slf4j
public class ImageUtil {

    public static void main(String[] args) {
        addWatermark("static/red.jpg", "static/temp/qrcode.jpg");
    }

    /**
     * 添加水印图片
     * @param sourcePath 源图片路径
     * @param addPath 水印图片路径
     */
    public static void addWatermark(String sourcePath, String addPath){
        try {
            BufferedImage original = ImageIO.read(new ClassPathResource(sourcePath).getInputStream());
            BufferedImage watermark = ImageIO.read(new ClassPathResource(addPath).getInputStream());

            // 这里用画图工具计算图片上二维码的尺寸是 142*142
            double scaleRatio = Math.min(142d / watermark.getWidth(), 142d / watermark.getHeight());
            int scaleWidth = (int) (watermark.getWidth() * scaleRatio);
            int scaleHeight = (int) (watermark.getHeight() * scaleRatio);

            Image scaledInstance = watermark.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH);

            BufferedImage scaledWatermark = new BufferedImage(scaleWidth, scaleHeight, watermark.getType());
            // 缩放图片
            Graphics2D g1 = scaledWatermark.createGraphics();
            g1.drawImage(scaledInstance, 0, 0, scaleWidth, scaleHeight, null);
            g1.dispose();

            // 原始图片上添加图片水印
            Graphics2D g = original.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g.drawImage(scaledInstance, 448, 971, null);

            String filePath = + System.currentTimeMillis() + ".png";

            File file = new File("/Users/warmchuang/codes/person/mini-wework/src/main/resources/static/" + filePath);
            ImageIO.write(original, "png", file);
            g.dispose();
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    private static Image scaleImage(InputStream source, int w, int h){
        try {
            BufferedImage sourceImage = ImageIO.read(source);
            // 这里用画图工具计算图片上二维码的尺寸是 142*142
            double scaleRatio = Math.min(w * 1.0 / sourceImage.getWidth(), h * 1.0 / sourceImage.getHeight());
            int scaleWidth = (int) (sourceImage.getWidth() * scaleRatio);
            int scaleHeight = (int) (sourceImage.getHeight() * scaleRatio);

            Image scaledInstance = sourceImage.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH);

            BufferedImage scaledWatermark = new BufferedImage(scaleWidth, scaleHeight, sourceImage.getType());
            // 缩放图片
            Graphics2D g1 = scaledWatermark.createGraphics();
            g1.drawImage(scaledInstance, 0, 0, scaleWidth, scaleHeight, null);
            g1.dispose();
            return scaledInstance;
        } catch (Exception e){
            log.info("scale image error:{}", e, e.getMessage());
        }
        return null;
    }

    public static void addWatermark(BufferedImage source, Image watermark, int x, int y){
        try {
            // 原始图片上添加图片水印
            Graphics2D g = source.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
            g.drawImage(watermark, x, y, null);
            g.dispose();
        } catch (Exception e){
            log.info("add watermark error:{}", e, e.getMessage());
        }
    }

    public static byte[] addWatermark(InputStream watermark, JsonConfig.Poster poster){
        ByteArrayOutputStream os = null;
        try {
            os = new ByteArrayOutputStream();

            BufferedImage original = ImageIO.read(new ClassPathResource(poster.getSourceImg()).getInputStream());
            Image scaledInstance = scaleImage(watermark, poster.getWatermarkWidth(), poster.getWatermarkWidth());

            addWatermark(original, scaledInstance, poster.getXAxis(), poster.getYAxis());

            ImageIO.write(original, "png", os);
            return os.toByteArray();
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static String addWatermark(InputStream watermark, JsonConfig.Poster poster, String filename){
        try {
            BufferedImage original = ImageIO.read(new ClassPathResource(poster.getSourceImg()).getInputStream());
            Image scaledInstance = scaleImage(watermark, poster.getWatermarkWidth(), poster.getWatermarkWidth());

            addWatermark(original, scaledInstance, poster.getXAxis(), poster.getYAxis());

            File file1 = new File(filename);
            file1.createNewFile();

            ImageIO.write(original, "png", file1);
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
